package xyz.ddlnt.servicegeneral.controller;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import xyz.ddlnt.commonutil.result.Result;
import xyz.ddlnt.model.vo.FileUploadVO;
import xyz.ddlnt.servicegeneral.util.MinIOUtils;


/**
 * @author ZZULI_SE 210910
 * @data 2025/4/23 17:47
 */
@Slf4j
@RestController
@RequestMapping("/minio")
public class MinioController {


    @Resource
    private MinIOUtils minioUtil;

    /**
     * 上传文件
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public Result<FileUploadVO> uploadFile(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        try {
            // 确保请求体被完全读取
            request.getInputStream().skip(request.getContentLength());
        }catch (Exception e) {
            log.error("service-general：请求体读取失败：{}", e.getMessage());
        }
        log.info("上传文件：{}", file.getOriginalFilename());
        return Result.success( minioUtil.minioUpload(file));
    }
    /**
     * 下载文件
     * @param fileName
     * @param response
     * @return
     */
    @RequestMapping("/downloadFile")
    public String downloadFile(@RequestParam String fileName, HttpServletResponse response) {
        return minioUtil.downloadFile("data-enpower",fileName,response) != null ? "下载成功" : "下载失败";
    }

    /**
     * 删除文件
     *
     * @param fileName 文件路径
     * @return
     */
    @PostMapping("/deleteFile")
    public Result<Boolean> deleteFile(String fileName) {
//        Boolean flag = minioUtil.deleteFile("data-enpower",fileName);
        return Result.success(true);
    }
}
